let socketServer = require('./modules/socket_server');

let str = "<cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";
let buf = Buffer.alloc(256);
buf.write(str, 0, str.length+4);
let mapSockets = new Map();

function recvCallback(s,data) {
    // console.log(data);
    // if (data=="<policy-file-request/>") {
        try {
            s.write(buf);
            // console.log("send <cross-domain-policy>");
        } catch (err) {
            console.error("socket send error "+err);
        }
    // }
}
function connCallback(s) {
    // console.log(data);
    mapSockets.set(s, Date.now()+10000);
}
function closeCallback(s,err) {
    mapSockets.delete(s);
}

let server = new socketServer(843);
server.startListen(connCallback, null, recvCallback, closeCallback);

// setInterval(() => {
//     let now = Date.now();
//     console.log("check socket timeout, total:"+mapSockets.size);
//     mapSockets.forEach(function (value, key, map) {
//         if (value<now) {
//             key.end();
//             console.log("socket timeout!");
//         }
//     });
// }, 60000);